# -*- coding = utf-8 -*-
# @Time : 2021/12/13 14:09
# @Author : liuguoliang
# @File : log.py
# @Software: PyCharm

import os
import logging
from time import strftime
# import warnings


class log:

    def __init__(self):
        # os.chdir(os.path.dirname(os.path.dirname(__file__)))  # 切换到当前路径的主目录
        # print(os.getcwd())
        os.chdir('../Log')
        name = strftime('%F')  # 日志名称
        self.filename = os.path.join('{}.log'.format(name))  # 保存日志的路径

    def start_log(self, level, message):
        """添加日志器"""
        logger = logging.getLogger()
        logger.setLevel(logging.INFO)
        logger.handlers.clear()

        """添加过滤器"""
        File = logging.FileHandler(self.filename, 'a', encoding='utf-8')
        File.setLevel(logging.INFO)

        output = logging.StreamHandler()
        output.setLevel(logging.INFO)

        """添加格式器"""
        Format = logging.Formatter('%(asctime)s-%(filename)s-%(funcName)s-%(lineno)s-->%(levelname)s : %(message)s')
        File.setFormatter(Format)
        output.setFormatter(Format)

        """添加处理器"""
        logger.addHandler(File)
        logger.addHandler(output)

        """关闭文件"""
        File.close()
        output.close()

        """通过判断定义收集日志的级别"""
        if level == 'info':
            logger.info(message)

        if level == 'error':
            logger.error(message)

        if level == 'debug':
            logger.debug(message)

        if level == 'warming':
            logger.warning(message)

        os.chdir('../Runner')

        """以下方法为设置收集日志的各个级别"""

    def info(self, message):
        self.start_log('info', message)

    def error(self, message):
        self.start_log('error', message)

    def debug(self, message):
        self.start_log('debug', message)

    def warming(self, message):
        self.start_log('warming', message)


if __name__ == '__main__':
    log().info('aaa')
